package com.buobao.oa.controller;

import com.buobao.oa.entity.Department;
import com.buobao.oa.entity.Employee;
import com.buobao.oa.entity.Node;
import com.buobao.oa.entity.User;
import com.buobao.oa.service.DepartmentService;
import com.buobao.oa.service.EmployeeService;
import com.buobao.oa.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "IndexServlet",urlPatterns = "/index")
public class IndexServlet extends HttpServlet {
    private UserService userService = new UserService();

    private EmployeeService employeeService = new EmployeeService();
    private DepartmentService departmentService = new DepartmentService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session == null) {
            resp.sendRedirect("/timeover.html");
            return;
        }
        User user = (User) session.getAttribute("login_user");
        if (user == null) {
            resp.sendRedirect("/timeover.html");
        } else {
            List<Node> nodeList = userService.getNodeListByUserId(user.getUserId());
            req.setAttribute("node_list",nodeList);
            Employee employee = employeeService.selectByEmployeeId(user.getEmployeeId());
            session.setAttribute("current_employee",employee);
            Department department = departmentService.getDepartmentById(employee.getDepartmentId());
            session.setAttribute("current_department",department);
            req.getRequestDispatcher("index.ftl").forward(req,resp);
        }
    }
}
